package org.sky.service.serviceImpl;

import org.sky.base.DaoSupport;
import org.sky.model.Authorities;
import org.sky.model.Authority;
import org.sky.model.Person;
import org.sky.service.AuthoritiesService;
import org.sky.service.PersonService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.Query;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-10-21
 * Time: 下午8:29
 * To change this template use FileUtil | Settings | FileUtil Templates.
 */

@Service
@Transactional
public class PersonServiceImpl extends DaoSupport<Person> implements PersonService {
    private AuthoritiesService authoritiesService;

    public Person findPersonByUserName(String userName) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userName", userName);
        return this.findSpecialObject(Person.class, params);
    }

    @Override
    public void setPersonEnable(String[] userNames, boolean enable) {
        if (userNames != null && userNames.length > 0) {
            StringBuffer jpql = new StringBuffer();
            for (int i = 0; i < userNames.length; i++) {
                jpql.append('?').append((i + 2)).append(',');
            }
            jpql.deleteCharAt(jpql.length() - 1);
            Query query = em.createQuery("update Person person set person.enabled = ?1 where person.userName in (" + jpql
                    .toString() + ")");
            query.setParameter(1, enable);
            for (int i = 0; i < userNames.length; i++) {
                query.setParameter(i + 2, userNames[i]);
            }
            query.executeUpdate();
        }
    }

    @Override
    public void save(Object entity) {
        if (entity instanceof Person) {
            Person person = (Person) entity;
            this.save(person, Authority.ROLE_USER);
        } else
            super.save(entity);
    }

    @Override
    public void save(Person person, Authority Authority) {
        super.save(person);
        authoritiesService.save(new Authorities(person, Authority));
    }

    @Override
    public void changeAuthority(Person person, Authority Authority) {
        Authorities authorities = authoritiesService.find(Authorities.class, person.getId());
        authorities.setAuthority(Authority);
        person.setAuthority(Authority.getName());
        authoritiesService.update(authorities);
    }

    public List<Person> allPerson() {
        Query query = em.createQuery("from Person p");
        return query.getResultList();
    }

    @Resource(name = "authoritiesServiceImpl")
    public void setAuthoritiesService(AuthoritiesService authoritiesService) {
        this.authoritiesService = authoritiesService;
    }

    /*不需要删除用户*/
/*    @Override
    public <T> void delete(Class<T> entityClass, Object entityId) {
        try {
            authoritiesService.delete(Authorities.class, entityId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.delete(entityClass, entityId);
    }

    @Override
    public void delete(Object entity) {
        if (entity instanceof Person) {
            Person person = (Person) entity;
            authoritiesService.delete(Authorities.class, person.getId());
        }
        super.delete(entity);
    }*/
}
